// pages/address/add.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    name_value: '',
    phone_value: '',
    region_value: '',
    address_value: '',
    uid: '',
    default_value: false,
    id: '',
    options: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
      options
    })
    this.setData({
      id: options.address_id
    })
    this.get_address_detail(options.address_id)
  },

  get_address_detail(id) {
    let params = {
      id
    }
    request.post(api.addressdetail, params).then(res => {
      let region_value = []
      region_value[0] = res.province_name
      region_value[1] = res.city_name
      region_value[2] = res.region_name

      this.setData({
        name_value: res.accept_name,
        phone_value: res.mobile,
        region_value,
        address_value: res.address,
        default_value: res.is_default,
      })
    })
  },

  delete_address() {
    util.showLoading("正在删除")
    let params = {
      ids: this.data.id
    }
    request.post(api.addressdelete, params).then(res => {
      let pages = getCurrentPages()
      let prev_page = pages[pages.length - 2]
      prev_page.onLoad(this.data.options)
      util.hideLoading()
      util.goBack()
    })
  },

  switching(e) {
    this.setData({
      default_value: e.detail.value
    })
  },

  picking_region(e) {
    this.setData({
      region_value: e.detail.value
    })
  },

  save_address() {
    if (this.data.name_value == '') {
      util.showToast('请输入您的姓名')
      return
    }
    if (this.data.phone_value == '') {
      util.showToast('请输入您的电话号码')
      return
    }
    if (this.data.region_value == '') {
      util.showToast('请输入您的所在地区')
      return
    }
    if (this.data.address_value == '') {
      util.showToast('请输入您的详细地址')
      return
    }
    let params = {
      id: this.data.id,
      uid: this.data.uid,
      name: this.data.name_value,
      mobile: this.data.phone_value,
      province_name: this.data.region_value[0],
      city_name: this.data.region_value[1],
      region_name: this.data.region_value[2],
      address: this.data.address_value,
      is_default: this.data.default_value ? 1 : 0
    }
    request.post(api.addressupdate, params).then(res => {
      let pages = getCurrentPages()
      let prev_page = pages[pages.length - 2]
      prev_page.onLoad()
      util.goBack()
    })
  },

  address_input(e) {
    this.setData({
      address_value: e.detail.value
    })
  },

  name_input(e) {
    this.setData({
      name_value: e.detail.value
    })
  },

  phone_input(e) {
    this.setData({
      phone_value: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})